package com.shujia.flink.tf;

import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.*;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.SlidingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;

public class Demo6Window {
    public static void main(String[] args) throws Exception{

        /*
         *1、实时统计单词的数量，每隔5秒统计一次(滑动时间)，统计最近15的单词（窗口大小） （滑动窗口）
         */

        //创建flink环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //读取数据
        DataStream<String> lines = env.socketTextStream("master", 8888);

        //一行转换成多行，转换成kv格式
        DataStream<Tuple2<String, Integer>> kvs = lines.flatMap((line, collect) -> {
            String sep = ",";
            for (String word : line.split(sep)) {
                collect.collect(Tuple2.of(word, 1));
            }
        }, Types.TUPLE(Types.STRING, Types.INT));

        //按照单词分组
        KeyedStream<Tuple2<String, Integer>, String> keyBy = kvs.keyBy(kv -> kv.f0);

        /*
         * 划分窗口,使用滑动窗口
         * 实时统计单词的数量，每隔5秒统计一次(滑动时间)，统计最近15的单词（窗口大小） （滑动窗口）
         * SlidingProcessingTimeWindows: 滑动的处理时间窗口
         */
        WindowedStream<Tuple2<String, Integer>, String, TimeWindow> window = keyBy
                .window(SlidingProcessingTimeWindows.of(Time.seconds(15), Time.seconds(5)));

        //统计单词的数量
        DataStream<Tuple2<String, Integer>> count = window.sum(1);

        count.print();

        env.execute();
    }
}
